/**
 * Dynamic room selection for RoomBooking Template of RoomBooking View
 * 
 * @version 1.0
 * @package    Joomla
 * @subpackage RoomBooking
 * @copyright (C) 2011 Giovanni Marzulli
 * @license		GNU/GPL
 */

//set reload
jQuery(function(){
    var select_value = jQuery('#id_organization').attr('value');
    if(select_value != '')selBuildings(select_value);
});

function selBuildings(id_organization) {
	var jq = jQuery.noConflict();
	jq(document).ready(function() {									
			jq.get('index.php?option=com_roombooking&view=roombooking&layout=dyn_select', { organizationid: id_organization, requestItems: 'building'},
				function(dataBuildings){								   
					jq("tbody[id='id_building']").empty();
					jq("table[id='room_table']").attr({style:"display:none"});
					var rows = "";
                                        var cla = "";
					var from=dataBuildings.search("##Start-buildings##");
					var to=dataBuildings.search("##End-buildings##");
					var data = dataBuildings.substring(from+19,to);
					var arrayBuildings = data.split( '||');
					for (var i = 1; i < arrayBuildings.length; i++) {
						var building = arrayBuildings[i].split( '**');
                                                if(i%2==1) cla = "row0";
                                                else cla = "row1";
						rows += "<tr id='"+building[0]+"' class='"+cla+"' onclick=selRooms("+building[0]+")><td>" + i + "</td><td>" + building[1] + "</td><td>"+ building[2] + "</td><td>"+ building[3] + "</td><td>"+ building[4] +"</td><td>"+ building[5] +"</tr>";
					}
					jq("tbody[id='id_building']").html(rows);
					jq("table[id='building_table']").attr({style:"display:inline"});
				}
			);			
	});
}

function selRooms(id_building) {
	var jq = jQuery.noConflict();
	jq(document).ready(function() {
            //css
            jq('#id_building tr').css({'color':'#484848'});
            jq('#id_building tr[id='+id_building+']').css({'color':'red'});
            //css
			jq.get('index.php?option=com_roombooking&view=roombooking&layout=dyn_select', { buildingid: id_building, requestItems: 'room'},
				function(dataRooms){								   
					jq("tbody[id='id_room']").empty();
					var rows = "";
                                        var cla = "";
					var from=dataRooms.search("##Start-rooms##");
					var to=dataRooms.search("##End-rooms##");
					var data = dataRooms.substring(from+15,to);
					var arrayRooms = data.split( '||');
					for (var i = 1; i < arrayRooms.length; i++) {
						var room = arrayRooms[i].split( '**');
                                                if(i%2==1) cla = "row0";
                                                else cla = "row1";
						rows+= "<tr class='"+cla+"' onclick=window.location.href='index.php?option=com_roombooking&view=bookings&id_room="+room[0]+"'><td>"+ i +"</td><td>" + room[1] + "</td><td>" + room[2] +"</td><td>" + room[3] +"</td><td>" + room[4] +"</td></tr>";
						
					}
					jq("tbody[id='id_room']").html(rows);
					jq("table[id='room_table']").attr({style:"display:inline"});
				}
			);			
	});
}